package com.wjbgn.bsolver.server.controller;

import cn.hutool.core.date.DateUtil;
import com.wjbgn.bsolver.server.convert.BugAnsweredRecordDoConvert;
import com.wjbgn.bsolver.server.dto.BugAnsweredRecordDTO;
import com.wjbgn.bsolver.server.entity.BugAnsweredRecordDO;
import com.wjbgn.bsolver.server.service.IBugAnsweredRecordService;
import com.wjbgn.bsolver.server.service.IBugRecordService;
import com.wjbgn.bsolver.server.util.UserInfoCacheUtil;
import com.wjbgn.stater.dto.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Description:
 * Create Date: 2022-04-21T13:26:52.898
 *
 * @author wjbgn
 * @version 1.0
 */
@Api(tags = "")
@RestController
@RequestMapping("/answered")
@AllArgsConstructor
public class BugAnsweredRecordController {

    /**
     * BugAnsweredRecordService
     */
    private IBugAnsweredRecordService bugAnsweredRecordService;

    private IBugRecordService bugRecordService;

    private UserInfoCacheUtil userInfoCacheUtil;

    /**
     * list列表
     *
     * @param bugAnsweredRecordDTO
     * @return
     */
    @ApiOperation(value = "list列表")
    @GetMapping("/list")
    public Result list(BugAnsweredRecordDTO bugAnsweredRecordDTO) {
        List<BugAnsweredRecordDTO> list = bugAnsweredRecordService.list(BugAnsweredRecordDoConvert.dtoToDo(bugAnsweredRecordDTO));
        // 每获取一次bug明细列表，就更新一次bug查看次数
        bugRecordService.updateBugViews(bugAnsweredRecordDTO.getBugId(), bugAnsweredRecordDTO.getMurmur());
        return Result.success(list);
    }

    /**
     * 新增
     *
     * @param bugAnsweredRecordDTO
     * @return
     */
    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public Result save(@RequestBody BugAnsweredRecordDTO bugAnsweredRecordDTO) {
        BugAnsweredRecordDO save = bugAnsweredRecordService.save(BugAnsweredRecordDoConvert.dtoToDo(bugAnsweredRecordDTO));
        //更新回复数量
        bugRecordService.updateBugAnswers(bugAnsweredRecordDTO.getBugId());
        BugAnsweredRecordDTO answeredRecordDTO = BugAnsweredRecordDoConvert.doToDto(save);
        answeredRecordDTO.setCreateTimeStr(DateUtil.format(answeredRecordDTO.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
        answeredRecordDTO.setNickname(userInfoCacheUtil.getUserNicknameByUsername(answeredRecordDTO.getUsername()));
        answeredRecordDTO.setBeAnsweredNickname(userInfoCacheUtil.getUserNicknameByUsername(answeredRecordDTO.getBeAnsweredUsername()));
        return Result.success(answeredRecordDTO);
    }


}